import getRoutes from './router/index.js';
const CompressionPlugin = require("compression-webpack-plugin");
const cheerio = require('cheerio');
export default {
  // Global page headers: https://go.nuxtjs.dev/config-head
  head: {
    htmlAttrs: {
      lang: 'en'
    },
    meta: [{
      charset: 'utf-8'
    },
    {
      name: 'viewport',
      content: 'width=device-width, initial-scale=1'
    },
    { name: 'applicable-device', content: 'pc' }
    ],
    link: [{
      rel: 'icon',
      type: 'images/x-icon',
      href: '/images/about/icon/logo.png'
    }],
    script: [{
      src: "/js/flexible.js",
      type: "text/javascript",
      charset: "utf-8"
    }, {
      src: "/js/jquery-1.11.3.min.js",
      type: "text/javascript",
      body: true,
      charset: "utf-8"
    }, {
      src: "/js/hmbaidu.js",
      type: "text/javascript",
      body: true,
      charset: "utf-8"
    }, {
      src: "/js/qrcode/utf.js",
      type: "text/javascript",
      body: true,
      charset: "utf-8"
    }, {
      src: "/js/qrcode/jquery.qrcode.js",
      type: "text/javascript",
      body: true,
      charset: "utf-8"
    },
    //全局引用商务通
    // {
    //   src: "https://plt.zoosnet.net/JS/LsJS.aspx?siteid=PLT78689288&float=1&lng=cn",
    //   type: "text/javascript",
    //   body: true,
    //   charset: "utf-8"
    // },
      // {
      //   src: "https://api.map.baidu.com/api?v=3.0&ak=07RDeC1CPGmvID0XmlpnfsnrDCrUsjzm",
      //   type: "text/javascript",
      //   body: true,
      //   charset: "utf-8"
      // }
    ],
  },
  // Global CSS: https://go.nuxtjs.dev/config-css
  css: [
    '@/xya/css/common.css',
    '@/xya/css/contain.css',
    // "@/static/font-awesome-4.7.0/css/font-awesome.min.css"
  ],
  // Plugins to run before rendering page: https://go.nuxtjs.dev/config-plugins
  plugins: [
    '~/plugins/axios',
    '@/plugins/mixins.js',
    '@/plugins/component.js',
    '@/plugins/main',
    //'@/plugins/wxkf.js',
  ],
  // Auto import components: https://go.nuxtjs.dev/config-components
  components: true,

  // Modules for dev and build (recommended): https://go.nuxtjs.dev/config-modules
  buildModules: [],

  // Modules: https://go.nuxtjs.dev/config-modules
  modules: [
    '@nuxtjs/axios',
    '@nuxtjs/proxy'
  ],
  axios: {
    timeout: 30000, //超时时间
    proxy: true,
    prefix: '/api'
  },
  proxy: {
    '/api': {
      // target: 'http://192.168.0.184:8080/', //要代理的后端地址
      target: 'http://127.0.0.1:8080/',
      pathRewrite: {
        '^/api': ''
      },
      changeOrigin: true,
    }
  },
  // Build Configuration: https://go.nuxtjs.dev/config-build
  build: {
    extractCSS: true,
    extractJS: true,
    extend (config, ctx) {
      config.node = {
        fs: "empty"
      }
    },
    plugins: [
      new CompressionPlugin({
        test: /\.js$|\.html$|\.css/, // 匹配文件名
        threshold: 10240, // 对超过10kb的数据进行压缩
        deleteOriginalAssets: false, // 是否删除原文件
      }),
    ]
  },
  router: {
    extendRoutes: (routes, resolve) => {
      // 清空Nuxt.js自动生成的路由hmbaidu
      routes.splice(0);
      // 重新将自定义路由配置设置进去
      routes.push(...getRoutes(resolve));
    },
    middleware: 'midd'
  },
  hooks: {
    // 'render:route': (url, result) => {
    //   this.$ = cheerio.load(result.html, { decodeEntities: false });
    //   //由于window.__nuxt__总是位于body中的第一个script中，
    //   //所以我移除了body中第一个脚本标签
    //   this.$(`body script`).eq(0).remove();
    //   result.html = this.$.html()
    // },
    "render:route": (url, result) => {
      result.html = result.html.replace(/data-n-head=\"ssr\"/gi, '')
    },
    'vue-renderer:ssr:context' (context) {
      const routePath = JSON.stringify(context.nuxt.routePath)
      context.nuxt = { serverRendered: true, fetch: context.nuxt.fetch, routePath }
    }//解决asyncdata请求后源码拼接window.__NUXT__问题
  },
  // telemetry: false, //关闭提示
  render: {
    resourceHints: false,
  },
  nuxtPrecompress: {
    gzip: {
      enabled: true,
      filename: '[path].gz[query]',
      threshold: 10240,
      minRatio: 0.8,
      compressionOptions: {
        level: 9
      },
    },
    brotli: {
      enabled: true,
      filename: '[path].br[query]',
      compressionOptions: {
        level: 11
      },
      threshold: 10240,
      minRatio: 0.8,
    },
    enabled: true,
    report: false,
    test: /\.(js|css|html|txt|xml|svg)$/,
    // Serving options
    middleware: {
      enabled: true,
      enabledStatic: true,
      encodingsPriority: ['br', 'gzip'],
    },
  }
}